package com.bw.seckill_module.seckilllist;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.common.field.EventBusEntity;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.providerservice.CheckLoginApi;
import com.bw.common.utils.ToastShowUtils;
import com.bw.network_sdk.BaseEntity;
import com.bw.seckill_module.SecKillRespository;
import com.bw.seckill_module.entity.GoodsEntity;
import com.bw.seckill_module.entity.SecKillEntity;
import com.bw.seckill_module.entity.SecKillListEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SecKillListViewModel extends BaseViewModel<SecKillRespository> implements OnItemClickListener,
        OnItemChildClickListener {

    public ObservableField<String>titleStr = new ObservableField<>("");
    public ObservableField<List<GoodsEntity>>secKill=new ObservableField<>();
    public long time = 0l;
    private Map<String,Object>secMap;
    private CheckLoginApi checkLoginApi;
    @Override
    public void onResult(BaseEntity entity) {
        if (entity instanceof SecKillEntity){
            //秒杀成功
            ToastShowUtils.showShort(entity.message);
        }else if (entity instanceof SecKillListEntity){
            //秒杀列表
            SecKillListEntity secKillListEntity = (SecKillListEntity) entity;
            secKill.set(secKillListEntity.data);
            long t = secKillListEntity.data.get(0).nowtime;
            //获取秒杀列表接口成功->发消息事件到SeckilllistActivity->开启后台定时服务
            //维护接口中获取这条数据时的时间->接口时间
            Map<String,Object>map=new HashMap<>();
            map.put("time",t);
            EventBusEntity eventBusEntity = new EventBusEntity();
            eventBusEntity.action = "startTimmer";
            eventBusEntity.data=map;
            EventBus.getDefault().post(eventBusEntity);
        }
    }

    @Override
    protected void initData() {
        checkLoginApi = (CheckLoginApi) ARouter.getInstance()
                .build("/user/checklogin").navigation();
        respository.requestSecKillList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }
    //立刻秒杀
    public void secKill(){
        secMap.put("time",time);
        respository.requestSecKill(secMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (!checkLoginApi.checkLogin()){
            //跳转登录界面
            ARouter.getInstance().build("/user/loginactivity")
                    .navigation();
            return;
        }
        GoodsEntity entity = (GoodsEntity) adapter.getData().get(position);
        secMap = new HashMap<>();
        secMap.put("goodsCount","1");
        secMap.put("goodsDesc",entity.goods_desc);
        secMap.put("goodsIcon",entity.goods_default_icon);
        secMap.put("goodsId",entity.id);
        secMap.put("goodsPrice",entity.goods_default_price);
        secMap.put("userId",checkLoginApi.getUserId());
        //发布eventbus事件到activity获取关联的service下维护的时间
        EventBusEntity eventBusEntity = new EventBusEntity();
        eventBusEntity.action = "secTime";
        EventBus.getDefault().post(eventBusEntity);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        //跳转详情
        GoodsEntity entity = (GoodsEntity) adapter.getData().get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("goodsvalue",entity);
        startBundPage("/seckill/seckillvalueactivity",bundle);
    }
}
